define(["router"],function(router){
	"use strict";
	//time format
    Date.prototype.format = function (fmt) { 
        var o = {
            "M+": this.getMonth() + 1, //月份 
            "d+": this.getDate(), //日 
            "h+": this.getHours(), //小时 
            "m+": this.getMinutes(), //分 
            "s+": this.getSeconds(), //秒 
            "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
            "S": this.getMilliseconds() //毫秒 
        };
        if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    }

    /*directive start*/
    function newGuid(){
	    var guid = "";
	    for (var i = 1; i <= 32; i++){
	      var n = Math.floor(Math.random()*16.0).toString(16);
	      guid +=   n;
	      if((i==8)||(i==12)||(i==16)||(i==20))
	        guid += "-";
	    }
	    return guid;    
	}
	router.directive("btDialog",function(){
		var config = {
			restrict:"E",
			transclude: true,
			scope:{
				options:"="
			},
			templateUrl:"/framework/template/dialog.html",
			link:function(scope,element,attr){
				
				scope._id = newGuid();
				var id = "#" + scope._id;
				scope.showDialog = function(){
					$(id).modal("show")
				}

				scope.$watch("options",function(newValue,oldValue){
					scope._title = newValue.titleName || '';
					scope._btnSave = newValue.btnSaveName || "save";
					scope._cancel = newValue.btnCancel || "";
					scope._btnShowName = newValue.butShowName || "modal";
					scope._displayNoBtn = newValue.displayNoBtn || "";
					if (scope._displayNoBtn) {
						$(id).modal("show");
					};

					$(id).on("show.bs.modal",function(){
						if (typeof newValue.beforeShow == "function") {
							newValue.beforeShow();
						};
					});
					scope.save = function(){
						if (newValue.afterShow && typeof newValue.afterShow == "function") {
							newValue.afterShow();
							$(id).modal("hide");
						};
					}

				},true);
			}
		}
		return config;
	});

	router.directive("btTooltips",["$timeout",function($timeout){
		var config = {
			restrict:"EAC",
			templateUrl:"framework/template/tooltips.html",
			// replace:true,
			scope:{
				options:"=",
			},
			link:function(scope,element,attr){
				var self = $(element[0]);
				self.tooltip();

				//1、位置
				//2、内容
				//3、显示时间长度
				var cancel = undefined;
				scope.$watch("options",function(n,o){
					console.log(n,"new...")
					scope._text = n.text || "";
					scope._time = n.time || 2000;
					scope._position = n.position || "top";
					scope._btnclick = n.btnclick || "";
					scope._btnName = n.btnName || "button";
					scope.btnclick = function(){
						if (scope._btnclick && typeof scope._btnclick == "function") {
							scope._btnclick();
						};
					}

					if (scope.options.text == "") return;

					self.attr("data-placement",scope._position);
					self.attr("data-original-title",scope._text);
					self.tooltip("show");

					var cancel = $timeout(function(){
						console.log("tooltips....")
						self.tooltip("hide");
						scope._text = "" ;
						scope.options.text = "";
					},scope._time);
				},true);
				
			}
		};
		return config;
	}]);
	

	router.directive("btCollapse",function(){
		return {
			restrict: "E",
			templateUrl:"framework/template/collaspe.html",
			scope:{
				options:"="
			},
			link:function(scope,element,attr){
				scope.$watch("options",function(n,o){
					console.log(n)
					scope._content = n.content || [];
				});
				
			}
		};
	});

	router.directive("btTable",function(){
		return {
			restrict: "E",
			templateUrl:"framework/template/table.html",
			scope:{
				data:"=",
				rows:"=columns",
				td:"=",
				tr:"="
			},
			link:function(scope,element,attr){
				console.log(scope.data)
				console.log(scope.rows)
				console.log(scope.td)

			}
		}
	});

	router.directive("btTemplate",function(){
		return {
			restrict:"E",
			link:function(scope,element,attr){
				element.hide()
				router.setAttr(scope,attr.column,element.html())
			}
		}
	});

	//valid
	router.directive("valid",function(){
		var config = {
			restrict:"EAC",
			scope:{
				"ngModel":"="
			},
			link:function(scope,element,attrs){
				// attrs.
				console.log("ngmodel...");
				scope.watch("ngModel",function(n,o){
					console.log(n,"n...");
					console.log(o,"o...")

				});
				scope.$watch("ngModel",function(n,o){
					console.log(n,"nnnnn...")
					console.log(o,"ooooo...")
				});
			}
		};
		return config;
	});




	return router;
});